package com.changgou.seckill.service.impl;

import com.changgou.seckill.pojo.SeckillGoods;
import com.changgou.seckill.service.SecKillGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SecKillGoodsServiceImpl implements SecKillGoodsService {

    /***
     * 根据时间段,查询秒杀商品列表
     * @param time
     * @return
     */

    @Autowired
    private RedisTemplate redisTemplate;

    //redis key开头
    public static final String SECKILL_GOODS_KEY = "seckill_goods_";
    public static final String SECKILL_GOODS_STOCK_COUNT_KEY = "seckill_goods_stock_count_";

    @Override
    public List<SeckillGoods> list(String time) {
        //从redis里面获取数据,seckill_goods_2020042610 1
        List<SeckillGoods> seckillGoodsList = redisTemplate.boundHashOps(SECKILL_GOODS_KEY + time).values();

        //更新库存数据的来源
        for (SeckillGoods seckillGoods : seckillGoodsList) {
            String result = (String) redisTemplate.opsForValue().get(SECKILL_GOODS_STOCK_COUNT_KEY + seckillGoods.getId());
            seckillGoods.setStockCount(Integer.parseInt(result));
        }
        return seckillGoodsList;
    }
}
